package com.wanmait.myhouse.dao;

import com.wanmait.myhouse.util.DBHelper;
import com.wanmait.myhouse.vo.FeedBack;
import com.wanmait.myhouse.vo.Mediator;
import com.wanmait.myhouse.vo.Pager;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.taglibs.standard.extra.spath.Step;

import java.sql.SQLException;
import java.util.List;

public class FeedBackDAO {

    public List<FeedBack> selectByUserId(Integer id , Pager pager){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        int start = (pager.getPageNO() - 1) * pager.getPageSize();
        String sql = "select * from t_feedback where userId=?  limit ?,?";
        try {
            List<FeedBack> feedBackList = queryRunner.query(sql, new BeanListHandler<>(FeedBack.class),id,start,pager.getPageSize());
            return feedBackList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public void updateById(FeedBack feedBack){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "update t_feedback set reply=? , dispose=? where id=?";
        try {
            queryRunner.update(sql,feedBack.getReply(),feedBack.getDispose(),feedBack.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public FeedBack selectById(Integer id){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select * from t_feedback where id=?";
        try {
            FeedBack feedBack = queryRunner.query(sql, new BeanHandler<>(FeedBack.class),id);
            return feedBack;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public List<FeedBack> selectAll(Pager pager){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select * from t_feedback order by dispose asc limit ?,?";
        int start = (pager.getPageNO()-1)*pager.getPageSize();
        try {
            List<FeedBack> feedBackList = queryRunner.query(sql, new BeanListHandler<>(FeedBack.class), start, pager.getPageSize());
            return feedBackList ;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public int getTotal(){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select count(*) as total from t_feedback ";
        try {
            Object o = queryRunner.query(sql, new ScalarHandler<>());
            int total = Integer.parseInt(o.toString());
            return (int) total;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    public int getTotal(FeedBack feedBack){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select count(*) as total from t_feedback where userId = ?";
        try {
            Object o = queryRunner.query(sql, new ScalarHandler<>() , feedBack.getUserId());
            int total = Integer.parseInt(o.toString());
            return (int) total;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    public void addFeedBack(FeedBack feedBack){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "insert into t_feedback (userId,mediatorId,feedBack,dispose,time) value(?,?,?,?,?)";
        try {
            queryRunner.update(sql,feedBack.getUserId(),feedBack.getMediatorId(),feedBack.getFeedBack(),feedBack.getDispose(),feedBack.getTime());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
